package com.lanhai.crowd.mvc.handler;

import com.github.pagehelper.PageInfo;
import com.lanhai.crowd.entity.Role;
import com.lanhai.crowd.service.api.RoleService;
import com.lanhai.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ResponseBody
    @RequestMapping("/role/get/data/info.json")
    public ResultEntity<PageInfo<Role>> getRolePageInfo(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword) {

        // 1.调用getPageInfo方法
        PageInfo<Role> pageInfo = roleService.getPageInfo(keyword, pageNum, pageSize);

        return ResultEntity.data(pageInfo);
    }

    @ResponseBody
    @RequestMapping("/role/save.json")
    public ResultEntity<String> saveRole(Role role) {

        // 保存role
        roleService.saveRole(role);

        return ResultEntity.ok();
    }


    @ResponseBody
    @RequestMapping("/role/edit.json")
    public ResultEntity<String> editRole(Role role) {

        // 修改role
        roleService.editRole(role);

        return ResultEntity.ok();
    }

    @ResponseBody
    @RequestMapping("/role/remove/by/role/id/remove.json")
    public ResultEntity<String> editRole(@RequestBody List<Integer> roleIds) {

        // 根据IdList删除role
        roleService.deleteRoleByIds(roleIds);

        return ResultEntity.ok();
    }

}
